package cn.daliyTest.com.thread.processCommunication;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.ReentrantLock;

/**
 * ReentrantLock 可重入锁
 *
 * @author ：ljf
 * @date ：Created in 2021/6/24 11:36
 */
public class TestSyncByReentrantLock {
    public static void main(String[] args) {
        ReentrantLock lock = new ReentrantLock();
        Condition condition = lock.newCondition();

        List<String> list = new ArrayList<>();

        Thread threadA = new Thread(() -> {
            lock.lock();
            for (int i = 0; i < 10; i++) {
                list.add("aaa");
                System.out.println("A add ,size:" + list.size());
                if(list.size() == 5){
                    condition.signal();
                }
            }
            lock.unlock();
        });

        Thread threadB = new Thread(()->{
            lock.lock();
            if(list.size() != 5){
                try {
                    condition.await();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }

            }
            System.out.println("b start");
            lock.unlock();
        });

        threadB.start();
        threadA.start();
    }


}
